package thread;

public class Wait {

    public static void main(String[] args) {
        final Object obj = new Object();
        char[] number = {'1','2','3','4','5','6','7','8','9'};
        char[] letter = {'A','B','C','D','E','F','G','H','I'};

        new Thread(() -> {
            synchronized (obj){
                for(char num : number){
                    System.out.print(num + " ");
                    try {
                        obj.notify(); //叫醒其他线程，这里就是t2
                        obj.wait(); //让自己阻塞，让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                obj.notify(); //必须要有，因为两个线程的try里面的最后一步是阻塞，如果线程执行完了还在阻塞肯定不对，必须要唤醒，才能正确结束程序
            }
        }).start();

        new Thread(() -> {
            synchronized (obj){
                for(char let : letter){
                    System.out.print(let + " ");
                    try {
                        obj.notify(); //叫醒其他线程，这里是t1
                        obj.wait(); //让自己阻塞，让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                obj.notify(); //同上
            }
        }).start();
    }

}
